package com.sizzlevoice.sizzlehome;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ReadXMLFile {
	
	private Document doc;
	private boolean fileFound = true;
	private boolean fileFormat = true;
	private List<String> recipes = new ArrayList<String>();
	private HashMap<String, List<String>> map;
	
	ReadXMLFile(String filePath){
	  try {
		  
		  //Create a new HashMap
		  map = new HashMap<String, List<String>>();
		  
		  //----------------------------------------------------------------
		  //Create a new file with the specified path. Parse the file
		  //and get the DOM object document. Normalize the DOM object to 
		  //allows direct access to the child node that is the root element 
		  //of the document
		  //----------------------------------------------------------------
		  File recipeXmlFile = new File(filePath);
		  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		  doc = dBuilder.parse(recipeXmlFile);
		  doc.getDocumentElement().normalize();
		  parse();
		
	  } catch(FileNotFoundException fileNotFoundException){
		  fileFound = false;
	  }catch (Exception e) {
		e.printStackTrace();
	  }
	}
	
	/**
	 * Parse NotesXML file and store words and definitions
	 * in HashMap
	 */
	public void parse(){
		NodeList nList = doc.getElementsByTagName("recipe");
 
		//search all recipe tags
		for (int i = 0; i < nList.getLength(); i++) {
			
			//Get the first recipe tag
			Node nNode = nList.item(i);
		   
			//Check if the nNode is of type element node
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				//Convert nNode to an element
				Element eElement = (Element) nNode;
				
				//Store word and definition in HashMap
				String recipeName = eElement.getChildNodes().item(0).getNodeValue();
				String ingredients = getDefValue("ingredients", eElement);
				String directions = getDefValue("directions", eElement);
				List<String> l = new ArrayList<String>();
				l.add(ingredients);
				l.add(directions);
				map.put(recipeName, l);
				recipes.add(recipeName);

		   }else{
			   fileFormat = false;
		   }
		}
	}
	
	public int getRecipeCount(){
		NodeList nList = doc.getElementsByTagName("recipe");
		return nList.getLength();
	}
	
	public List<String> getRecipeNames(){
		return recipes;
	}
	
	
	public String getWord(int i){
		return recipes.get(i);
	}
	
	public List<String> getIngAndDir(String key){
		return map.get(key);
	}
	
	public boolean getFileFound(){
		return fileFound;
	}
	
	private static String getDefValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		Node nValue = (Node) nlList.item(0);
		return nValue.getNodeValue();
		}
}
